package com.itboye.lottery.fragment.home.shuangseqiu


import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.GridLayoutManager
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.itboye.bylibrary.util.TimeUtils
import com.itboye.lottery.R
import com.itboye.lottery.activity.WebActivity
import com.itboye.lottery.activity.shuangseqiu.ShuangSeQiuMainActivity
import com.itboye.lottery.adapter.ShuangSeQiuCodeAdapter
import com.itboye.lottery.alert.ByAlert
import com.itboye.lottery.base.BaseFragment
import com.itboye.lottery.bean.CodeBean
import com.itboye.lottery.bean.ColorBallPrizeHisBean
import com.itboye.lottery.presenter.UserPresenter
import com.itboye.lottery.retrofit.ResponseCallback
import com.itboye.lottery.utils.CodeUtil.initCodeData
import com.itboye.lottery.utils.Combination
import com.itboye.lottery.utils.ConstUrl
import kotlinx.android.synthetic.main.fragment_shuangseqiu.*


/**
 * A simple [Fragment] subclass.
 */
class ShuangSeQiuFragment : BaseFragment(), ResponseCallback {


    var type = 0
    var userPresenter: UserPresenter? = null
    var tv_shuangseqiu_message_1: TextView? = null
    var tv_colorball_play_method: TextView? = null

    var redAdapter: ShuangSeQiuCodeAdapter? = null
    var redTuoMaAdapter: ShuangSeQiuCodeAdapter? = null
    var blueAdapter: ShuangSeQiuCodeAdapter? = null

    companion object {
        fun newInstance(type: Int): ShuangSeQiuFragment {
            val fragment = ShuangSeQiuFragment()
            val bundle = Bundle()
            bundle.putInt("type", type)
            fragment.arguments = bundle
            return fragment
        }

    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_shuangseqiu
    }

    override fun initData() {
        userPresenter = UserPresenter(this)
        //获取开奖历史
        userPresenter!!.colorBallPrizeHistory()
        initCodeType()
        initCode()
        initRecyclerViewData()
    }

    var arrayRedCode: ArrayList<CodeBean>? = null
    var arrayRedTuoCode: ArrayList<CodeBean>? = null
    var arrayBlueCode: ArrayList<CodeBean>? = null

    private fun initCode() {
        arrayRedCode = initCodeData(33, type)//红色
        arrayRedTuoCode = initCodeData(33, 1)//红色脱码
        arrayBlueCode = initCodeData(16, 2)//蓝色球

    }

    private fun initCodeType() {
        type = arguments!!.getInt("type")//0：普通投注  1：胆拖投注
        rl_blue_tuo.visibility = View.GONE
        tv_blue.text = "蓝球"
        tv_blue_count.text = "最少选择1个"
        when (type) {
            0 -> {
                ll_tuoma.visibility = View.GONE
                tv_red_count.text = "最少选择6个"
            }
            1 -> {
                ll_tuoma.visibility = View.VISIBLE
                tv_red.text = "胆码:红球"
                tv_red_count.text = "最少选择1个,最多选择5个"
                tv_red_tuoma.text = "拖码:红球"
                tv_red_tuoma_count.text = "最少选择2个"
            }
            else -> {
            }
        }
    }


    private fun initRecyclerViewData() {
        redAdapter = ShuangSeQiuCodeAdapter(this, true, arrayRedCode)
        rv_red_ball.layoutManager = GridLayoutManager(activity, 7)
        rv_red_ball.adapter = redAdapter

        rv_blue_ball.layoutManager = GridLayoutManager(activity, 7)
        blueAdapter = ShuangSeQiuCodeAdapter(this, false, arrayBlueCode)
        rv_blue_ball.adapter = blueAdapter

        if (type == 1) {
            //只有胆拖投注才显示
            redTuoMaAdapter = ShuangSeQiuCodeAdapter(this, true, arrayRedTuoCode)
            rv_red_ball_tuoma.layoutManager = GridLayoutManager(activity, 7)
            rv_red_ball_tuoma.adapter = redTuoMaAdapter
        }
        redAdapter!!.setOnItemClickListener { view, position ->
            if (!arrayRedCode!![position].isSelect) {
                //普通投注红球个数>=6
                if (type == 0) {

                }
                //胆拖红球1-5
                if (type == 1) {
                    arrayRedTuoCode!!.forEachIndexed { index, codeBean ->
                        if (codeBean.isSelect and (codeBean.codeValue == arrayRedCode!![position].codeValue)) {
                            ByAlert.alert("胆码与拖码不能相等")
                            return@setOnItemClickListener
                        }
                    }
                    var selectCount = 0
                    arrayRedCode!!.forEachIndexed { index, codeBean ->
                        if (codeBean.isSelect) {
                            selectCount++
                        }
                    }
                    if (selectCount >= 5) {
                        ByAlert.alert("红色球个数最多选择5个")
                        return@setOnItemClickListener
                    }
                }
            }
            arrayRedCode!![position].isSelect = !arrayRedCode!![position].isSelect
            redAdapter!!.notifyDataSetChanged()
            redAdapter!!.selectCodeArray
            caculateZhu()
        }
        redTuoMaAdapter?.setOnItemClickListener({ view, position ->
            if (!arrayRedTuoCode!![position].isSelect) {
                arrayRedCode!!.forEachIndexed { index, codeBean ->
                    if (codeBean.isSelect and (codeBean.codeValue == arrayRedTuoCode!![position].codeValue)) {
                        ByAlert.alert("胆码与拖码不能相等")
                        return@setOnItemClickListener
                    }
                }
            }
            //红球托码个数最少一个
            arrayRedTuoCode!![position].isSelect = !arrayRedTuoCode!![position].isSelect
            redTuoMaAdapter!!.notifyDataSetChanged()
            caculateZhu()
        })
        blueAdapter!!.setOnItemClickListener({ view, position ->
            arrayBlueCode!![position].isSelect = !arrayBlueCode!![position].isSelect
            blueAdapter!!.notifyDataSetChanged()
            caculateZhu()
        })
    }

    fun caculateZhu() {
        var arrayRed = redAdapter!!.selectCodeArray
        var arrayRedTuo = redTuoMaAdapter?.selectCodeArray
        var arrayBlue = blueAdapter!!.selectCodeArray
        //计算注数
        var num = 1
        var num2 = 1
        var num3 = Combination.findsort(arrayBlue, 1)
        when ((activity as ShuangSeQiuMainActivity).selectionPosition) {
            0 -> {
                num = Combination.findsort(arrayRed, 6)
                //普通投注 红胆蓝胆
                (activity as ShuangSeQiuMainActivity).setCalculateZhuShu(num * num3)
            }
            1 -> {
                //胆拖
                num = Combination.findsort(arrayRed, 5)
                num2 = Combination.findsort(arrayRedTuo, 6 - arrayRed.size)
                //胆拖
                (activity as ShuangSeQiuMainActivity).setCalculateZhuShu(num * num2 * num3)
            }
            else -> {
            }
        }
    }


    fun getSelcectCount(): Array<Int> {
        //[红色个数，红色托马数，蓝色个数]
        var redCount = 0
        arrayRedCode!!.forEachIndexed { index, codeBean ->
            if (codeBean.isSelect) {
                redCount++
            }
        }
        var redTuoCount = 0
        arrayRedTuoCode!!.forEachIndexed { index, codeBean ->
            if (codeBean.isSelect) {
                redTuoCount++
            }
        }
        var blueCount = 0
        arrayBlueCode!!.forEachIndexed { index, codeBean ->
            if (codeBean.isSelect) {
                blueCount++
            }
        }
        return arrayOf(redCount, redTuoCount, blueCount)
    }

    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.tv_colorball_play_method -> {
                startActivity(Intent(activity, WebActivity::class.java).putExtra("title", "玩法说明").putExtra("url", ConstUrl.play_method_url+"?type="+ConstUrl.TYPE_SHUANGSEQIU))
            }
            else -> {
            }
        }
    }

    private var colorBallPrizeHisBean: ColorBallPrizeHisBean? = null

    override fun success(o: Any, requestSuccessType: String) {
        when (requestSuccessType) {
            userPresenter!!.colorBallPrizeHistory_SUCCESS -> {
                colorBallPrizeHisBean = o as ColorBallPrizeHisBean
                if (colorBallPrizeHisBean!=null) {
                    try {
                        setHistoryData()
                    }catch (e:Exception){

                    }
                }
            }

            else -> {
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun setHistoryData() {
        if (colorBallPrizeHisBean!!.newest!=null) {
            tv_shuangseqiu_message_1!!.text = "第${colorBallPrizeHisBean!!.newest.phaseno}期 ${TimeUtils.getFormatTime(colorBallPrizeHisBean!!.newest.localendtime.toLong() * 1000, "yyyy-MM-dd HH:mm:ss")}截止投注"
        }
        //拼接红球蓝球
        var str = StringBuffer()
        if (colorBallPrizeHisBean!!.his!!.size > 0) {
            var textView = TextView(activity)
            textView.text = "第" + colorBallPrizeHisBean!!.his!![0].number + "期 "
            tv_shuangseqiu_message_2.addView(textView)
            colorBallPrizeHisBean!!.his!![0].red_num.forEachIndexed { index, s ->
                var textView = TextView(activity)
                textView.setBackgroundResource(R.drawable.solid_circle_border_red_small)
                textView.text = "$s"
                textView.gravity= Gravity.CENTER
                textView.setTextColor(resources.getColor(R.color.white))
                val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                layoutParams.setMargins(10, 10, 0, 10)
                tv_shuangseqiu_message_2.addView(textView,layoutParams)
            }
            colorBallPrizeHisBean!!.his!![0].blue_num.forEachIndexed { index, s ->
                var textView = TextView(activity)
                textView.setBackgroundResource(R.drawable.solid_circle_border_blue_small)
                textView.text = "$s"
                textView.gravity= Gravity.CENTER
                textView.setTextColor(resources.getColor(R.color.white))
                val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                layoutParams.setMargins(10, 10, 0, 10)
                tv_shuangseqiu_message_2.addView(textView,layoutParams)
            }
        }
    }

    override fun fail(code: Int, msg: Any, requestErrorType: String) {
        when (requestErrorType) {
            userPresenter!!.colorBallPrizeHistory_FAIL -> {
                ByAlert.alert(msg)
            }
            else -> {
            }
        }

    }

}
